package com.roam.cofig;

import com.roam.entity.Visits;
import io.micrometer.common.util.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class VisitsListTypeHandler implements TypeHandler<List<Visits>> {

    @Override
    public void setParameter(PreparedStatement ps, int i, List<Visits> parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null) {
            ps.setNull(i, Types.VARCHAR);
        } else {
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < parameter.size(); j++) {
                Visits visit = parameter.get(j);
                sb.append(visit.getDate()).append(",");
                sb.append(visit.getSceneId()).append(",");
                sb.append(visit.getScene()).append(",");
                sb.append(visit.getCity());
                if (j < parameter.size() - 1) {
                    sb.append(";");
                }
            }
            ps.setString(i, sb.toString());
        }
    }

    @Override
    public List<Visits> getResult(ResultSet rs, String columnName) throws SQLException {
        String resultString = rs.getString(columnName);
        if (StringUtils.isNotEmpty(resultString)) {
            List<Visits> visitsList = new ArrayList<>();
            String[] visitsArray = resultString.split(";");
            for (String visitString : visitsArray) {
                String[] visitFields = visitString.split(",");
                Visits visit = new Visits(visitFields[0], Integer.parseInt(visitFields[1]), visitFields[2], visitFields[3]);
                visitsList.add(visit);
            }
            return visitsList;
        }
        return new ArrayList<>();
    }

    @Override
    public List<Visits> getResult(ResultSet rs, int columnIndex) throws SQLException {
        String resultString = rs.getString(columnIndex);
        if (StringUtils.isNotEmpty(resultString)) {
            List<Visits> visitsList = new ArrayList<>();
            String[] visitsArray = resultString.split(";");
            for (String visitString : visitsArray) {
                String[] visitFields = visitString.split(",");
                Visits visit = new Visits(visitFields[0], Integer.parseInt(visitFields[1]), visitFields[2], visitFields[3]);
                visitsList.add(visit);
            }
            return visitsList;
        }
        return new ArrayList<>();
    }

    @Override
    public List<Visits> getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String resultString = cs.getString(columnIndex);
        if (StringUtils.isNotEmpty(resultString)) {
            List<Visits> visitsList = new ArrayList<>();
            String[] visitsArray = resultString.split(";");
            for (String visitString : visitsArray) {
                String[] visitFields = visitString.split(",");
                Visits visit = new Visits(visitFields[0], Integer.parseInt(visitFields[1]), visitFields[2], visitFields[3]);
                visitsList.add(visit);
            }
            return visitsList;
        }
        return new ArrayList<>();
    }
}
